using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NTTHAO.CommonWeb;
using System.IO;

public partial class admin_webparts_Common_FileUploader : System.Web.UI.UserControl
{
    public string[] _UploadedFilesList = new string[0];
    public bool[] _IsImageFiles = new bool[0];
    public string[] _UploadedFilesExtention = new string[0];

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!Directory.Exists(Server.MapPath("../" + Constant.UPLOAD_FILE_PATH)))
            {
                Directory.CreateDirectory(Server.MapPath("../" + Constant.UPLOAD_FILE_PATH));
            }
            if (Request.QueryString["action"] != null)
            {
                if (Request.QueryString["action"].Equals("delete", StringComparison.OrdinalIgnoreCase))
                {
                    string fileNameDelete = Request.QueryString["file"];
                    if (fileNameDelete != null)
                    {
                        if (File.Exists(Server.MapPath("../" + Constant.UPLOAD_FILE_PATH + fileNameDelete)))
                        {
                            File.Delete(Server.MapPath("../" + Constant.UPLOAD_FILE_PATH + fileNameDelete));
                        }
                    }
                }
                Response.Redirect("?type=upload");
            }
            this.lblUploadedFilePath.Text = LanguageUtility.GetLanguageText("NONE", Session);
            this.lnkUploadedFile.Text = LanguageUtility.GetLanguageText("NONE", Session);
            LoadFiles();
        }
    }

    private void LoadFiles()
    {
        try
        {
            _UploadedFilesList = Directory.GetFiles(Server.MapPath("../" + Constant.UPLOAD_FILE_PATH));
            _IsImageFiles = new bool[_UploadedFilesList.Length];
            _UploadedFilesExtention = new string[_UploadedFilesList.Length];
            for (int i = 0; i < _UploadedFilesList.Length; i++)
            {
                _UploadedFilesList[i] = Path.GetFileName(_UploadedFilesList[i]);
                _UploadedFilesExtention[i] = Path.GetExtension(_UploadedFilesList[i]).ToLower();

                if (Constant.IMAGE_FILE_EXTENTIONS.Contains(_UploadedFilesExtention[i]))
                {
                    _IsImageFiles[i] = true;
                }
                else
                {
                    _IsImageFiles[i] = false;
                }
            }
        }
        catch (Exception)
        {
        }
    }

    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (this.fulFile.PostedFile != null
            && this.fulFile.FileName != null
            && this.fulFile.FileName.Length > 0)
        {
            try
            {
                string imageName = this.fulFile.FileName.Replace(" ", "");
                if (System.IO.File.Exists(Server.MapPath("../" + Constant.UPLOAD_FILE_PATH + imageName)))
                {
                    System.IO.File.Delete(Server.MapPath("../" + Constant.UPLOAD_FILE_PATH + imageName));
                }
                this.fulFile.PostedFile.SaveAs(Server.MapPath("../" + Constant.UPLOAD_FILE_PATH + imageName));

                lnkUploadedFile.NavigateUrl = "../" + Constant.UPLOAD_FILE_PATH + imageName;
                lnkUploadedFile.Text = imageName;
                lblUploadedFilePath.Text = (Request.ApplicationPath.Trim().EndsWith("/") ? Request.ApplicationPath : (Request.ApplicationPath + "/")) + Constant.UPLOAD_FILE_PATH + imageName;
            }
            catch (Exception)
            {
                //Response.Write(ex.ToString());
                //Response.Write(ex.StackTrace);
                return;
            }
        }
        else
        {
            lnkUploadedFile.NavigateUrl = "";
            this.lblUploadedFilePath.Text = LanguageUtility.GetLanguageText("NONE", Session);
            this.lnkUploadedFile.Text = LanguageUtility.GetLanguageText("NONE", Session);
        }

        LoadFiles();
    }
}
